/*
 * Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.xiaominfo.com.
 * Developer Web Site: http://open.xiaominfo.com.
 */

package com.yuanshi.food.common.config;

import cn.hutool.core.util.RandomUtil;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * *
 * 创建Swagger配置
 *
 * @author wcy
 * @date 2024/11/28
 */
@Configuration
public class SwaggerConfig {
    /**
     * 根据@Tag 上的排序，写入x-order
     *
     * @return the global open api customizer
     */
    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            if (openApi.getTags() != null) {
                openApi.getTags().forEach(tag -> {
                    Map<String, Object> map = new HashMap<>(1);
                    map.put("x-order", RandomUtil.randomInt(0, 100));
                    tag.setExtensions(map);
                });
            }
            if (openApi.getPaths() != null) {
                openApi.addExtension("x-test123", "333");
                openApi.getPaths().addExtension("x-abb", RandomUtil.randomInt(1, 100));
            }
        };
    }

    /**
     * 自定义开放api
     *
     * @return {@link OpenAPI }
     */
    @Bean
    public OpenAPI customOpenApi() {
        return new OpenAPI()
                .info(new Info()
                        .title("元气食光轻食小程序后端服务文档")
                        .version("1.0")
                        .description( "环境来到元气食光")
                        .termsOfService("https://doc.xiaominfo.com")
                        .license(new License().name("Apache 2.0")
                                .url("https://doc.xiaominfo.com")));
    }

    @Bean
    public GroupedOpenApi commonApi() {
        return GroupedOpenApi.builder()
                             .group("littleSecretary-common")
                             .displayName("小秘-公共")
                             .pathsToMatch("/**")
                             .packagesToScan("org.wcy.trigger.http.common")
                             .build();
    }

    @Bean
    public GroupedOpenApi webApi() {
        return GroupedOpenApi.builder()
                             .group("littleSecretary-web")
                             .displayName("小秘-用户端")
                             .pathsToMatch("/**")
                             .packagesToScan("org.wcy.trigger.http.web")
                             .build();
    }

    @Bean
    public GroupedOpenApi mgrApi() {
        return GroupedOpenApi.builder()
                             .group("littleSecretary-mgr")
                             .displayName("小秘-管理端")
                             .pathsToMatch("/**")
                             .packagesToScan("org.wcy.trigger.http.mgr")
                             .build();
    }


}
